package org.wmine.preprocess;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Arc2D.Float;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;

import org.wmine.core.FloatTextField;
import org.wmine.data.Attribute;


public class MinMax extends JFrame implements ActionListener
{
	DefaultTableModel dtm;
	JLabel lblMin,lblMax,lblAttr,lblInfo;
	FloatTextField txtMin,txtMax;
	JButton btnNormalize;
	JComboBox cmbAttr;
	
	public MinMax(String []columnNames,DefaultTableModel dtm) 
	{
		
		this.dtm=dtm;
		this.setLayout(null);
		this.setBounds(150,150,500,300);
		this.setTitle("Min-Max Normalization");
		
		lblAttr=new JLabel("Select Attribute");
		lblAttr.setBounds(20,50,200,25);
		
		cmbAttr=new JComboBox();
		for(int i=0;i<columnNames.length;i++)
			cmbAttr.addItem(columnNames[i]);
		cmbAttr.setBounds(230,50,150,25);
		cmbAttr.addActionListener(this);
		lblMin=new JLabel("Enter New Minimum Value");
		lblMin.setBounds(20,90,200,25);
		
		lblMax=new JLabel("Enter New Maximum Value");
		lblMax.setBounds(20,130,200,25);
	
		txtMin=new FloatTextField();
		txtMin.setBounds(230,90,150,25);
		
		txtMax=new FloatTextField();
		txtMax.setBounds(230,130,150,25);
		
		btnNormalize=new JButton("Normalize");
		btnNormalize.setBounds(180,185,100,25);
		btnNormalize.addActionListener(this);
		
		
		lblInfo=new JLabel("Note");
		lblInfo.setBounds(50,230,400,25);
		
		this.add(lblAttr);
		this.add(cmbAttr);
		this.add(lblMax);
		this.add(lblMin);
		this.add(txtMax);
		this.add(txtMin);
		this.add(btnNormalize);
		this.add(lblInfo);
		this.repaint();
		
	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
		
		int index=cmbAttr.getSelectedIndex();
		
		
		Discretization d=new Discretization(this.dtm);
		float oldmin=d.Minimum(index);
		float oldmax=d.Maximum(index);
		if(arg0.getSource()==cmbAttr)
		{	
			lblInfo.setText("Note:  Old Minimum Value: "+oldmin+"  ,  Old Maximum Value: "+oldmax);
			PreprocessPanel.report.append("<br>You chose to Perform Min-Max Normalization<br>");
			PreprocessPanel.report.append("<br>Old Minimum Value: "+oldmin+"&nbsp;&nbsp;&nbsp;&nbsp;Old Maximum Value: "+oldmax);
			PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
			PreprocessPanel.summary.repaint();
			this.repaint();
		}
		if(arg0.getSource()==btnNormalize)
		{
			float newmin=java.lang.Float.parseFloat(txtMin.getText());
			float newmax=java.lang.Float.parseFloat(txtMax.getText());
		
			PerformMinMax(newmin,newmax,oldmin,oldmax,index);
						
			this.dispose();
		}
	}
	
	void PerformMinMax(float newmin,float  newmax,float  oldmin,float  oldmax,int index)
	{
		Object []obj=new Object[this.dtm.getRowCount()];
		for(int i=0;i<this.dtm.getRowCount();i++)
		{
			float value=java.lang.Float.parseFloat(this.dtm.getValueAt(i,index).toString());
			float norm_value=(((value-oldmin)/(oldmax-oldmin)))*(newmax-newmin)+newmin;
			obj[i]=norm_value;
			PreprocessPanel.report.append("<br>"+value+" &nbsp;&nbsp;is normalized to&nbsp;&nbsp; ==>&nbsp;&nbsp;"+norm_value);
		}
		AddColumn(obj,index);
		PreprocessPanel.report_summary.setText(PreprocessPanel.report.toString());
		PreprocessPanel.summary.repaint();
	}

	void AddColumn(Object[] obj, int index) {
		// TODO Auto-generated method stub
		System.gc();
		System.runFinalization();
		this.dtm.addColumn("Normalized_"+cmbAttr.getSelectedItem(),obj);
		Vector v=new Vector();
		v=this.dtm.getDataVector();
		int cnt=this.dtm.getColumnCount();
		//System.out.println("v:"+v+"  :: cnt:: "+cnt);
		Vector v1=new Vector(cnt);
		for(int ii=0;ii<cnt;ii++)
			v1.add(this.dtm.getColumnName(ii));
		System.out.println("v1::"+v1);
		v1.removeElementAt(index);
		System.out.println("v1::"+v1);
		
		Iterator itr=v.iterator();
		while(itr.hasNext())
		{
			Vector o=(Vector)itr.next();
			o.removeElementAt(index);
			System.out.println("o::"+o);
		}
		this.dtm.setDataVector(v,v1);
	}
	
	public String getSelectedColumn()
	{
		return cmbAttr.getSelectedItem().toString();
	}
}